home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 December / PCWDEC06.iso / Software / Freeware / Gmail Manager 0.5.1 / gmanager051.xpi / components / gmManager.js < prev    next >
Encoding:
Text File  |  2006-08-28  |  9.2 KB  |  363 lines

  1. // Gmail Manager
  2. // By Todd Long <longfocus@gmail.com>
  3. // http://www.longfocus.com/firefox/gmanager/
  4.  
  5. const GM_CC = Components.classes;
  6. const GM_CI = Components.interfaces;
  7.  
  8. const GM_BRANCH = "longfocus.gmanager.";
  9. const GM_PASSWORD_SITE = "longfocus.gmanager.account";
  10. const GM_PROPERTIES = "chrome://gmanager/locale/gmanager.properties";
  11. const GM_VERSION = "0.5.1";
  12.  
  13. function gmManager() {}
  14. gmManager.prototype = {
  15.   _firstTime: true,
  16.   _parser: null,
  17.   _global: null,
  18.   _accounts: null,
  19.   
  20.   get branch() {
  21.     return GM_CC["@mozilla.org/preferences-service;1"]
  22.              .getService(GM_CI.nsIPrefService)
  23.              .getBranch(GM_BRANCH);
  24.   },
  25.   
  26.   get parser() {
  27.     if (!this._parser) {
  28.       try {
  29.         this._parser = GM_CC["@longfocus.com/gmanager/parser;1"].getService(GM_CI.gmIParser);
  30.         this._parser.load();
  31.       } catch(e) {
  32.         this._parser = null;
  33.       }
  34.     }
  35.     return this._parser;
  36.   },
  37.   
  38.   get version() {
  39.     return GM_VERSION;
  40.   },
  41.   
  42.   load: function()
  43.   {
  44.     if (this._firstTime)
  45.     {
  46.       // Load accounts
  47.       this._loadAccounts();
  48.       
  49.       // Set first time
  50.       this._firstTime = false;
  51.     }
  52.   },
  53.   
  54.   save: function()
  55.   {
  56.     for (var email in this._accounts)
  57.     {
  58.       var account = this._accounts[email];
  59.       var oldNode = account.prefs.node;
  60.       var newNode = null;
  61.       
  62.       // Save preferences
  63.       account.prefs.save();
  64.       
  65.       // Get new node
  66.       newNode = account.prefs.node;
  67.       
  68.       if (newNode.getAttribute("type") != "global")
  69.       {
  70.         newNode.setAttribute("type", account.type);
  71.         newNode.setAttribute("email", account.email);
  72.         newNode.setAttribute("alias", account.alias);
  73.         
  74.         if (account.remember)
  75.           this._storePassword(account.email, account.password);
  76.         else
  77.           this._removePassword(account.email);
  78.       }
  79.       
  80.       // Replaces old node
  81.       this.parser.doc.replaceChild(newNode, oldNode);
  82.     }
  83.     
  84.     // Save file
  85.     this.parser.save(null);
  86.   },
  87.   
  88.   importFile: function(aWindow, aFile)
  89.   {
  90.     var file = this._filePicker(aWindow, "import");
  91.     
  92.     if (file != null)
  93.     {
  94.       if (this.parser.open(file))
  95.       {
  96.         // Load accounts
  97.         this._loadAccounts();
  98.         
  99.         // Save accounts
  100.         this.save();
  101.       }
  102.       else
  103.         file = "";
  104.     }
  105.     
  106.     return file;
  107.   },
  108.   
  109.   exportFile: function(aWindow)
  110.   {
  111.     var file = this._filePicker(aWindow, "export");
  112.     
  113.     if (file != null)
  114.       this.parser.save(file);
  115.     
  116.     return file;
  117.   },
  118.   
  119.   getAccounts: function(aCount)
  120.   {
  121.     var accounts = new Array();
  122.     
  123.     for (var email in this._accounts)
  124.       accounts.push(this._accounts[email]);
  125.     
  126.     aCount.value = accounts.length;       
  127.     
  128.     return accounts;
  129.   },
  130.   
  131.   getAccount: function(aEmail)
  132.   {
  133.     return this._accounts[aEmail];
  134.   },
  135.   
  136.   addAccount: function(aType, aEmail, aPassword, aRemember, aAlias)
  137.   {
  138.     // Get account node
  139.     var node = this.parser.account;
  140.     
  141.     // Add node
  142.     this.parser.doc.appendChild(node);
  143.     
  144.     // Add account
  145.     this._accounts[aEmail] = this._createService(aType, aEmail, aPassword, aRemember, aAlias, node);
  146.     
  147.     // Save
  148.     this.save();
  149.     
  150.     return true;
  151.   },
  152.   
  153.   removeAccount: function(aEmail)
  154.   {
  155.     // Removes the account from document
  156.     this.parser.doc.removeChild(this._accounts[aEmail].prefs.node);
  157.     
  158.     // Removes the account from list
  159.     delete this._accounts[aEmail];
  160.     
  161.     // Save accounts
  162.     this.save();
  163.   },
  164.   
  165.   updateAccount: function(aEmail, aPassword, aRemember, aAlias)
  166.   {
  167.     // Update account
  168.     this._accounts[aEmail].password = aPassword;
  169.     this._accounts[aEmail].remember = aRemember;
  170.     this._accounts[aEmail].alias = aAlias;
  171.     
  172.     // Save accounts
  173.     this.save();
  174.   },
  175.   
  176.   _loadAccounts: function()
  177.   {
  178.     var accounts = this.parser.doc.getElementsByTagName("account");
  179.     
  180.     // Create our list of accounts
  181.     this._accounts = new Array();
  182.     
  183.     for (var i = 0; i < accounts.length; i++)
  184.     {
  185.       // Account details
  186.       var node = accounts.item(i);
  187.       var type = node.getAttribute("type");
  188.       var email = "global";
  189.       var password = null;
  190.       var remember = null;
  191.       var alias = null;
  192.       
  193.       if (type != "global")
  194.       {
  195.         email = node.getAttribute("email");
  196.         password = node.getAttribute("password");
  197.         alias = node.getAttribute("alias");
  198.         
  199.         if (password == null)
  200.           password = this._findPassword(email);
  201.         else
  202.         {
  203.           // Remove password attribute
  204.           node.removeAttribute("password");
  205.           
  206.           if (password != "")
  207.             this._storePassword(email, password)
  208.           else
  209.             this._removePassword(email);
  210.         }
  211.         
  212.         remember = (password != "" ? true : false);
  213.       }
  214.       
  215.       this._accounts[email] = this._createService(type, email, password, remember, alias, node);
  216.     }
  217.   },
  218.   
  219.   _createService: function(aType, aEmail, aPassword, aRemember, aAlias, aNode)
  220.   {
  221.     var service = null;
  222.     var prefs = GM_CC["@longfocus.com/gmanager/prefs;1"].createInstance(GM_CI.gmIPrefs);
  223.     
  224.     try {
  225.       // Check service type
  226.       switch (aType)
  227.       {
  228.         case "global":
  229.           // Create service
  230.           service = GM_CC["@longfocus.com/gmanager/service;1"].createInstance(GM_CI.gmIService);
  231.           break;
  232.         case "gmail":
  233.           // Create Gmail service
  234.           service = GM_CC["@longfocus.com/gmanager/service/gmail;1"].createInstance(GM_CI.gmIServiceGmail);
  235.           break;
  236.         default:
  237.           break;
  238.       }
  239.       
  240.       // Prefs node
  241.       prefs.node = aNode;
  242.       
  243.       // Service details
  244.       service.type = aType;
  245.       service.email = aEmail;
  246.       service.password = aPassword;
  247.       service.remember = aRemember;
  248.       service.alias = aAlias;
  249.       service.prefs = prefs;
  250.     } catch(e) {
  251.       service = null;
  252.     }
  253.     
  254.     return service;
  255.   },
  256.   
  257.   _findPassword: function(aEmail)
  258.   {
  259.     try {
  260.       var host = {value:""};
  261.       var user = {value:""};
  262.       var password = {value:""};
  263.       var pmi = GM_CC["@mozilla.org/passwordmanager;1"].getService(GM_CI.nsIPasswordManagerInternal);
  264.       pmi.findPasswordEntry(GM_PASSWORD_SITE, aEmail, "", host, user, password);
  265.     } catch(e) {}
  266.     
  267.     return password.value;
  268.   },
  269.   
  270.   _storePassword: function(aEmail, aPassword)
  271.   {
  272.     try {
  273.       var pm = GM_CC["@mozilla.org/passwordmanager;1"].getService(GM_CI.nsIPasswordManager);
  274.       
  275.       if (aPassword != null && aPassword != "")
  276.         pm.addUser(GM_PASSWORD_SITE, aEmail, aPassword);
  277.     } catch (e) {}
  278.   },
  279.   
  280.   _removePassword: function(aEmail)
  281.   {
  282.     try {
  283.       var pm = GM_CC["@mozilla.org/passwordmanager;1"].getService(GM_CI.nsIPasswordManager);
  284.       pm.removeUser(GM_PASSWORD_SITE, aEmail);
  285.     } catch (e) {}
  286.   },
  287.   
  288.   _filePicker: function(aWindow, aMode)
  289.   {
  290.     var nsIFilePicker = GM_CI.nsIFilePicker;
  291.     var picker = GM_CC["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  292.     var bundle = GM_CC["@mozilla.org/intl/stringbundle;1"].getService(GM_CI.nsIStringBundleService).createBundle(GM_PROPERTIES);
  293.     var file = null;
  294.     
  295.     picker.appendFilters(nsIFilePicker.filterXML);
  296.     picker.defaultExtension = ".xml";
  297.     
  298.     switch (aMode)
  299.     {
  300.       case "export":
  301.         picker.init(aWindow, bundle.GetStringFromName("imports-prefs-export"), nsIFilePicker.modeSave);
  302.         break;
  303.       case "import":
  304.         picker.init(aWindow, bundle.GetStringFromName("imports-prefs-import"), nsIFilePicker.modeOpen);
  305.         break;
  306.     };
  307.     
  308.     if (picker.show() == nsIFilePicker.returnOK)
  309.       file = picker.file;
  310.     
  311.     return file;
  312.   },
  313.   
  314.   QueryInterface: function(iid)
  315.   {
  316.     if (!iid.equals(GM_CI.gmIManager) &&
  317.         !iid.equals(GM_CI.nsISupports))
  318.       throw Components.results.NS_ERROR_NO_INTERFACE;
  319.     return this;
  320.   }
  321. }
  322.  
  323. var myModule = {
  324.   firstTime: true,
  325.   
  326.   myCID: Components.ID("{bf43b6d0-f7dd-11da-974d-0800200c9a66}"),
  327.   myDesc: "Mail accounts manager",
  328.   myProgID: "@longfocus.com/gmanager/manager;1",
  329.   myFactory: {
  330.     createInstance: function (outer, iid) {
  331.       if (outer != null)
  332.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  333.       
  334.       return (new gmManager()).QueryInterface(iid);
  335.     }
  336.   },
  337.   
  338.   registerSelf: function (compMgr, fileSpec, location, type)
  339.   {
  340.     if (this.firstTime) {
  341.       this.firstTime = false;
  342.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  343.     }
  344.     
  345.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  346.     compMgr.registerFactoryLocation(this.myCID, this.myDesc, this.myProgID, fileSpec, location, type);
  347.   },
  348.  
  349.   getClassObject: function (compMgr, cid, iid)
  350.   {
  351.     if (!cid.equals(this.myCID))
  352.       throw Components.results.NS_ERROR_NO_INTERFACE;
  353.     
  354.     if (!iid.equals(Components.interfaces.nsIFactory))
  355.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  356.     
  357.     return this.myFactory;
  358.   },
  359.   
  360.   canUnload: function(compMgr) { return true; }
  361. };
  362.  
  363. function NSGetModule(compMgr, fileSpec) { return myModule; }